<!-- 功能：NFS输出弹窗 | 作者：menghao.yan | 邮箱：yxs2615@163.com | 时间：2023年08月21日 22:59:06 -->
<template>
  <!-- 弹出框 -->
  <a-modal v-model:open="visible" title="NFS输出" width="500px" class="y-modal" okText="创建" @ok="onSubmit" @cancel="onCancel">
    <a-form :label-col="{ span: 0 }" :wrapper-col="{span: 24}">
      <template v-for="(item, index) in modelRef.nfsShuChuArr" :key="item.id">
        <a-form-item :label="index + 1" v-bind="validateInfos[`nfsShuChuArr.${index}.nfsShuChu`]" :name="['nfsShuChuArr', index, 'nfsShuChu']">
          <a-row :gutter="12">
            <a-col flex="auto">
              <a-select v-model:value="item.nfsShuChu" placeholder="请选择NFS输出">
                <a-select-option value="root">root</a-select-option>
                <a-select-option value="opt">opt</a-select-option>
              </a-select>
            </a-col>
            <a-col flex="20px">
              <minus-circle-outlined @click="removeNfsShuChu(item)" v-show="modelRef.nfsShuChuArr.length>1" />
            </a-col>
          </a-row>
        </a-form-item>
      </template>
      <a-button type="link" class="y-btn" @click="addNfsShuChu" block>
        <template #icon>
          <plus-outlined />
        </template>
        添加NFS输出
      </a-button>
    </a-form>
  </a-modal>

</template>

<script setup>
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const { visible } = toRefs(props)

// 定义组件的事件
const emits = defineEmits(['update:visible'])

import { Form } from 'ant-design-vue'

const useForm = Form.useForm

const modelRef = reactive({
  nfsShuChuArr: [{ nfsShuChu: '' }]
})

const rulesRef = ref({
  'nfsShuChuArr.0.nfsShuChu': [{ required: true, message: '选择NFS输出' }]
})

const { resetFields, validate, validateInfos, mergeValidateInfo } = useForm(modelRef, rulesRef)

const onSubmit = () => {
  validate()
    .then(() => {
      console.log('NFS输出>>', toRaw(modelRef))
      onCancel()
    })
    .catch(err => {
      console.log('error', err)
    })
}

const onCancel = () => {
  // 触发自定义事件来通知父组件关闭弹窗
  emits('update:visible', false)
}

const addNfsShuChu = () => {
  modelRef.nfsShuChuArr.push({ nfsShuChu: '', id: Date.now() })
  let index = modelRef.nfsShuChuArr.length - 1
  rulesRef.value[`nfsShuChuArr.${index}.nfsShuChu`] = [{ required: true, message: '选择NFS输出' }]
}

const removeNfsShuChu = item => {
  let index = modelRef.nfsShuChuArr.indexOf(item)
  if (index !== -1) {
    delete rulesRef.value[`nfsShuChuArr.${index}.nfsShuChu`]
    modelRef.nfsShuChuArr.splice(index, 1)
  }
}
</script>

<style scoped>
.ant-row {
  margin-bottom: 10px;
}
</style>